(function( window, undefined ) {

var NodeRequest = function(name, callback) {

	var _name = name;
	var _callback = callback;
	var _type = "text/javascript";
	var _template = false;
	
	var _head = document.getElementsByTagName("head")[0];
	var _baseElement = document.getElementsByTagName("base")[0];
	if ( _baseElement ) {
		_head = baseElement.parentNode;
	};
	
	var _scriptURL = function( name ) {
		var components = name.toLowerCase().split(".");
		var componentType = components.shift();
		var classType = components.shift();
	
		if ( classType == undefined ) {
			return "/core/" + name;
		}
		else if ( componentType == "helper" ) {
			return "/helpers/" + name;
		}
		else if ( componentType == "prototype" ) {
			return "/prototypes/" + name;
		}
		else if ( classType == "template" ) {
			_template = true;

			return "/templates/" + componentType + "/" + name;
		}
		else {
			if ( classType == "model" || classType == "view" || classType == "controller" ) { // Application
				return "/applications/" + componentType + "/" + classType + "s/" + name;
			}
			else { // Library
				return "/libraries/" + componentType + "/" + name;
			}
		};
		
		return undefined;
	};

	var filePath = "";
	var fileName = "";

	return {
		onScriptLoad: function() {
			if ( _callback ) {
				_callback.apply(this, arguments);
			};
		},
		
		onScriptError: function() {
			console.error( "undefined class ", _name );
		},
		
		load: function() {
			var namespace = _scriptURL( _name );

			if ( _template ) {

				var self = this;

				$.get("./js" + namespace + ".html", function(data) { 
							$.template(_name, data);

							self.onScriptLoad.call( self );
				});

				return ;
			};

			var node = document.createElement("script");
			node.src = "./js" + namespace + ".js";
			node.type = _type;
			node.charset = "utf-8";
			//node.async = true;
			
			if ( node.attachEvent ) {
				node.attacheEvent("onreadystatechange", this.onScriptLoad );
			}
			else {
				node.addEventListener("load", this.onScriptLoad, false );
				node.addEventListener("error", this.onScriptError, false );
			};
			
			if ( ! node.src ) {
				return null;
			};
			
			if ( _baseElement ) {
				_head.insertBefore(node, _baseElement );
			}
			else {
				_head.appendChild(node);
			};
			
			return node;
		}
	}
};

Class.import = function( js ) {
	var request = new NodeRequest(js );
	request.load();
};

// Define
Class.define = function( name, callback ) {
	if ( Object.prototype.toString.call( name ) == "[object Array]" ) {
		var requires = name;
		
		var i = 0;
		var nextRequest = function() {
			var key = requires[i];
			var handler = ( i < requires.length - 1 ) ? function() {
				
				i = i + 1;
				
				nextRequest();
				
			} : callback;
			
			var request = new NodeRequest( key, handler );
			
			request.load();
		};
		nextRequest();
		
		/*
		var completed = 0;
		var handlerScriptLoaded = function() {
			completed = completed + 1;
			
			if ( requires.length == completed ) {
				callback.apply( this );
			};
		};
		
		for ( var i=0,l=requires.length; i<l; i++ ) {
			var request = new NodeRequest( requires[i], handlerScriptLoaded );
			request.load();
		};
		*/
		
		return;
	};

	var request = new NodeRequest(name, callback );
	request.load();
};

})(window);
